package javolution.context;

import javolution.util.FastMap;

/* loaded from: classes2.dex */
public abstract class ObjectFactory<T> {
    private static final Allocator NULL_ALLOCATOR = new Allocator() { // from class: javolution.context.ObjectFactory.1
        @Override // javolution.context.Allocator
        protected Object allocate() {
            return null;
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // javolution.context.Allocator
        public void recycle(Object obj) {
        }
    };
    private boolean _doCleanup = true;
    private Allocator<T> _allocator = NULL_ALLOCATOR;
    private ThreadLocal _localAllocator = new ThreadLocal() { // from class: javolution.context.ObjectFactory.2
        @Override // java.lang.ThreadLocal
        protected Object initialValue() {
            return ObjectFactory.NULL_ALLOCATOR;
        }
    };

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public static class Generic extends ObjectFactory {
        private static final FastMap CLASS_TO_FACTORY = new FastMap().setShared(true);
        private final Class _class;

        private Generic(Class cls) {
            this._class = cls;
        }

        /* JADX INFO: Access modifiers changed from: private */
        public static Generic newInstance(Class cls) {
            Generic generic = new Generic(cls);
            CLASS_TO_FACTORY.put(cls, generic);
            return generic;
        }

        @Override // javolution.context.ObjectFactory
        protected Object create() {
            try {
                return this._class.newInstance();
            } catch (IllegalAccessException unused) {
                throw new Error("Cannot access no-arg constructor for " + this._class.getName() + ", the factory should be set explicitly using ObjectFactory.setInstance");
            } catch (InstantiationException unused2) {
                throw new Error("Cannot instantiate no-arg constructor for " + this._class.getName() + ", the factory should be set explicitly using ObjectFactory.setInstance");
            }
        }
    }

    public static <T> ObjectFactory<T> getInstance(Class<T> cls) {
        ObjectFactory<T> objectFactory = (ObjectFactory) Generic.CLASS_TO_FACTORY.get(cls);
        return objectFactory != null ? objectFactory : Generic.newInstance(cls);
    }

    public static <T> void setInstance(ObjectFactory<T> objectFactory, Class<T> cls) {
        Generic.CLASS_TO_FACTORY.put(cls, objectFactory);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public void cleanup(T t) {
        this._doCleanup = false;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public abstract T create();

    public final Allocator<T> currentAllocator() {
        Allocator<T> allocator = (Allocator) this._localAllocator.get();
        if (allocator.user != null) {
            this._allocator = allocator;
            return allocator;
        }
        Allocator<T> allocator2 = AllocatorContext.getCurrent().getAllocator(this);
        this._localAllocator.set(allocator2);
        this._allocator = allocator2;
        return allocator2;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public final boolean doCleanup() {
        return this._doCleanup;
    }

    public final T object() {
        Allocator<T> allocator = this._allocator;
        if (allocator.user != Thread.currentThread()) {
            allocator = currentAllocator();
        }
        return allocator.next();
    }

    public final void recycle(T t) {
        currentAllocator().recycle(t);
    }
}
